/**
 * UsersController.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * May 4, 2015
 */
package com.ovt.doorbell.payment.api.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ovt.doorbell.payment.api.response.DoorbellPaymentAPIResult;
import com.ovt.doorbell.payment.common.model.JsonDocument;
import com.ovt.doorbell.payment.common.utils.CookieUtil;
import com.ovt.doorbell.payment.common.utils.DataConvertUtils;
import com.ovt.doorbell.payment.dao.config.AppProperties;
import com.ovt.doorbell.payment.dao.vo.PageInfo;
import com.ovt.doorbell.payment.dao.vo.User;
import com.ovt.doorbell.payment.service.RefundService;
import com.ovt.doorbell.payment.service.UserService;
import com.ovt.doorbell.payment.service.context.SessionContext;
import com.ovt.doorbell.payment.service.exception.ServiceException;
import com.ovt.order.util.entity.Refund;
import com.ovt.order.util.entity.RefundRequest;

/**
 * UsersController provides restful APIs of user
 * 
 * @Author zhi.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */

@Controller
@RequestMapping("/refund")
public class RefundController
{
    @Autowired
    private RefundService refundService;

    private static final JsonDocument SUCCESS = DoorbellPaymentAPIResult.SUCCESS;

    /**
     * Get refund request list.
     * 
     * @param page
     * @param limit
     * @param sortBy
     * @param order
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/listUnprocessed")
    @ResponseBody
    public JsonDocument getRefundRequestList(@RequestParam(required = false,
            defaultValue = "0") String page, @RequestParam(required = false,
            defaultValue = "20") String limit, @RequestParam(required = false,
            defaultValue = "id") String sortBy, @RequestParam(required = false,
            defaultValue = "asc") String order) throws ServiceException
    {
        PageInfo pageInfo =
                new PageInfo(DataConvertUtils.toInt(page),
                        DataConvertUtils.toInt(limit), sortBy, order);

        List<RefundRequest> userList = refundService.getRefundRequestList(pageInfo);

        return new DoorbellPaymentAPIResult(userList);
    }
    
    /**
     * Get refund list.
     * 
     * @param page
     * @param limit
     * @param sortBy
     * @param order
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/listProcessed")
    @ResponseBody
    public JsonDocument getRefundList(@RequestParam(required = false,
            defaultValue = "0") String page, @RequestParam(required = false,
            defaultValue = "20") String limit, @RequestParam(required = false,
            defaultValue = "id") String sortBy, @RequestParam(required = false,
            defaultValue = "asc") String order) throws ServiceException
    {
        PageInfo pageInfo =
                new PageInfo(DataConvertUtils.toInt(page),
                        DataConvertUtils.toInt(limit), sortBy, order);

        List<Refund> userList = refundService.getRefundList(pageInfo);

        return new DoorbellPaymentAPIResult(userList);
    }

    /**
     * Get refund request by order no.
     * 
     * @param email
     * @param password
     * @return
     * @throws ServiceException
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getByOrderNo")
    @ResponseBody
    public JsonDocument getRefundRequestByOrderNo(@RequestParam(required = false,
            defaultValue = "0") String page, @RequestParam(required = false,
            defaultValue = "20") String limit, @RequestParam(required = false,
            defaultValue = "id") String sortBy, @RequestParam(required = false,
            defaultValue = "asc") String order, @RequestParam String orderNo)
            throws ServiceException
    {
        PageInfo pageInfo =
                new PageInfo(DataConvertUtils.toInt(page),
                        DataConvertUtils.toInt(limit), sortBy, order);
        
        List<RefundRequest> userList = refundService.getRefundRequestByOrderNo(pageInfo, orderNo);
        return new DoorbellPaymentAPIResult(userList);
    }
    
    /**
     * Process refund
     * 
     * @return
     * @throws ServiceException
     */
    
    @RequestMapping(method = RequestMethod.POST, value = "/process")
    @ResponseBody
    public JsonDocument refund(List<Long> ids) throws ServiceException
    {
        refundService.refund(ids);
        return SUCCESS;
    }
    
    /**
     * Reject refund
     * 
     * @return
     * @throws ServiceException
     */
    
    @RequestMapping(method = RequestMethod.POST, value = "/reject")
    @ResponseBody
    public JsonDocument refundReject(List<Long> ids) throws ServiceException
    {
        refundService.refundReject(ids);
        return SUCCESS;
    }

}
